﻿$(function () {
    var jsonData = [];

    $.ajax({
        type: "post",
        url: "/Sys/SysMenuTreeList",
        success: function (res) {
            if (!res.Result) {
                swal("菜单树加载出错了..", res.Message, "error");
                return;
            } else {
                $.each(res.Data, function (i, a) {
                    var b = {
                        data: a,
                        id: a.Id,
                        icon: a.Icon,
                        text: a.Name
                    }
                    jsonData.push(b);
                    if (a.HasSubMenu) {
                        b.state = { "opened": true };
                        b.children = [];
                        $.each(a.SubMenus, function (j, c) {
                            var d = {
                                data: c,
                                id: c.Id,
                                icon: c.Icon,
                                text: c.Name
                            }
                            b.children.push(d);
                            if (c.HasSubMenu) {
                                c.state = { "opened": true };
                                d.children = [];
                                $.each(a.SubMenus, function (k, e) {
                                    var f = {
                                        data: e,
                                        id: e.Id,
                                        icon: e.Icon,
                                        text: e.Name
                                    }
                                    d.children.push(e);
                                });
                            }
                        });
                    };
                });
            }

            $(".menuTree").jstree({
                "core": {
                    "check_callback": true,
                    "data": jsonData
                },
                "plugins": ["dnd", "changed", "state"],
            }).on('move_node.jstree', function (e, data) {
                //移动后展开父级
                $.jstree.reference('.menuTree').open_node(data.parent);

            }).on("changed.jstree", function (e, data) {

            });
        }
    });
});